package com.tool.emulator.mos6502;

import com.tool.emulator.common.Util;

/**
 * Resiters.java @author tl 2011-6-19
 */
public abstract class Registers implements Constants6502, java.io.Serializable {
	public void setValues(Registers src) {
		this.setAC(src.getAC());
		this.setXR(src.getXR());
		this.setIR(src.getIR());
		this.setSP(src.getSP());
		this.setPC(src.getPC());
		this.setN(src.isN());
		this.setV(src.isV());
		this.setB(src.isB());
		this.setD(src.isD());
		this.setI(src.isI());
		this.setZ(src.isZ());
		this.setC(src.isC());
	}

	public Registers copy() {
		return new DefaultRegisters(this);
	}

	public abstract int getAC();

	public abstract int setAC(int aC);

	public abstract int getXR();

	public abstract int setXR(int xR);

	public abstract int getYR();

	public abstract int setYR(int yR);

	public abstract int getIR();

	public abstract void setIR(int iR);

	public abstract int getSP();

	public abstract int setSP(int sP);

	public abstract int getPC();

	public abstract void setPC(int pC);

	public abstract boolean isN();

	public abstract void setN(boolean n);

	public abstract boolean isV();

	public abstract void setV(boolean v);

	public abstract boolean isB();

	public abstract void setB(boolean b);

	public abstract boolean isD();

	public abstract void setD(boolean d);

	public abstract boolean isI();

	public abstract void setI(boolean i);

	public abstract boolean isZ();

	public abstract void setZ(boolean z);

	public abstract boolean isC();

	public abstract void setC(boolean c);

	public boolean getFlag(int mask) {
		return 0 != (this.getFlags() & mask);
	}

	public int getFlags() {
		int s = FLAG_REVERSE;
		if (isN()) s |= FLAG_N;
		if (isV()) s |= FLAG_V;
		if (isB()) s |= FLAG_B;
		if (isD()) s |= FLAG_D;
		if (isI()) s |= FLAG_I;
		if (isZ()) s |= FLAG_Z;
		if (isC()) s |= FLAG_C;
		return s;
	}

	public void setFlags(int s) {
		setN(0 != (s & FLAG_N));
		setV(0 != (s & FLAG_V));
		setB(0 != (s & FLAG_B));
		setD(0 != (s & FLAG_D));
		setI(0 != (s & FLAG_I));
		setZ(0 != (s & FLAG_Z));
		setC(0 != (s & FLAG_C));
	}

	public String toString(CPU6502 cpu) {
	    /*
	    StringBuilder sb = new StringBuilder(this.toString());
        sb.append(" STACK{");
        for(int i = this.getSP()+1;i<0xff;i+=2)
        {
            if(i>this.getSP()+1)
                sb.append(",");
            sb.append(Util.w2s(cpu.read2(CPU6502.SP_OFFSET + i)+1));
        }
        sb.append("}");
        return sb.toString();*/
		return this.toString();
	}

	@Override
	public String toString() {
		return "PC=" + Util.w2s(getPC())
				+ " AC=" + Util.b2s(getAC())
				+ " XR=" + Util.b2s(getXR())
				+ " YR=" + Util.b2s(getYR())
				+ " IR=" + Util.b2s(getIR())
				+ " SP=" + Util.b2s(getSP())
				+ " S=" + Util.b2s(getFlags())
				+ " N:" + (isN() ? 1 : 0)
				+ " V:" + (isV() ? 1 : 0)
				+ " B:" + (isB() ? 1 : 0)
				+ " D:" + (isD() ? 1 : 0)
				+ " I:" + (isI() ? 1 : 0)
				+ " Z:" + (isZ() ? 1 : 0)
				+ " C:" + (isC() ? 1 : 0);
	}

}
